package com.hxk.ioc.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 判断运行环境是否为wondow环境
 */
public class WindowsCondition implements Condition {
    /**
     * 判断方法
     * @param conditionContext 判断条件能使用的上下文（环境）
     * @param annotatedTypeMetadata 注释信息
     * @return
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取ioc使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();

        //获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();

        //获取当前的运行环境信息
        Environment environment = conditionContext.getEnvironment();

        //获取到bean定义的注册类
        BeanDefinitionRegistry registry = conditionContext.getRegistry();

        //获取资源信息
        ResourceLoader resourceLoader = conditionContext.getResourceLoader();

        //从运行环境中获取当前操作系统的名称
        String property = environment.getProperty("os.name");

        if(property.contains("Windows")){
            return true;
        }

        return false;
    }
}
